package com.ruoyi.traveladmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.traveladmin.mapper.TaTravelTargeMapper;
import com.ruoyi.traveladmin.service.ITaTravelTargeService;
import com.ruoyi.traveladmin.domain.TaTravelTarge;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.traveladmin.domain.TaTravel;
import com.ruoyi.traveladmin.service.ITaTravelService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TaTravelTargeServiceImpl implements ITaTravelTargeService {


    @Resource
    private TaTravelTargeMapper taTravelTargeMapper;
    
    
    @Resource
    private ITaTravelService taTravelService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param taTravelTarge 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<TaTravelTarge> getTaTravelTargePage(PageDomain pageDomain, TaTravelTarge taTravelTarge , TaTravel taTravel) {
        if (Objects.isNull(taTravelTarge)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<TaTravelTarge> queryWrapper = new LambdaQueryWrapper<>(new TaTravelTarge());
            //if(!Objects.isNull(taTravelTarge.getName())){
            //	queryWrapper.like(TaTravelTarge::getName,taTravelTarge.getName());
            //}
            

            if (!Objects.isNull(taTravelTarge.getId())) {
                queryWrapper.eq(TaTravelTarge::getId, taTravelTarge.getId());
            }

            if (!Objects.isNull(taTravelTarge.getTaTravelId())) {
                queryWrapper.eq(TaTravelTarge::getTaTravelId, taTravelTarge.getTaTravelId());
            }

            taTravel.setBaseEntityNull();
            taTravel.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(taTravel)) {
                List<Long> idList = taTravelService.getIdListByTaTravel(taTravel);
                queryWrapper.in(TaTravelTarge::getTaTravelId, idList);
            }


            if (!Objects.isNull(taTravelTarge.getTarget())) {
                queryWrapper.like(TaTravelTarge::getTarget, taTravelTarge.getTarget());
            }

            if (!Objects.isNull(taTravelTarge.getStatus())) {
                queryWrapper.like(TaTravelTarge::getStatus, taTravelTarge.getStatus());
            }

            if (!Objects.isNull(taTravelTarge.getDelFlag())) {
                queryWrapper.like(TaTravelTarge::getDelFlag, taTravelTarge.getDelFlag());
            }

            if (!Objects.isNull(taTravelTarge.getCreateBy())) {
                queryWrapper.like(TaTravelTarge::getCreateBy, taTravelTarge.getCreateBy());
            }

            if (!Objects.isNull(taTravelTarge.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravelTarge.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravelTarge.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelTarge::getCreateTime, taTravelTarge.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravelTarge.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravelTarge::getCreateTime, taTravelTarge.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelTarge.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravelTarge.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravelTarge::getCreateTime, taTravelTarge.getParams().get("CreateTimeBeginTime"), taTravelTarge.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelTarge.getUpdateBy())) {
                queryWrapper.like(TaTravelTarge::getUpdateBy, taTravelTarge.getUpdateBy());
            }

            if (!Objects.isNull(taTravelTarge.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravelTarge.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravelTarge.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelTarge::getUpdateTime, taTravelTarge.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravelTarge.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravelTarge::getUpdateTime, taTravelTarge.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelTarge.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravelTarge.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravelTarge::getUpdateTime, taTravelTarge.getParams().get("UpdateTimeBeginTime"), taTravelTarge.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelTarge.getRemark())) {
                queryWrapper.like(TaTravelTarge::getRemark, taTravelTarge.getRemark());
            }
        Page<TaTravelTarge> taTravelTargeList= taTravelTargeMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(TaTravelTarge record:taTravelTargeList.getRecords()){
            record.setTaTravel(taTravelService.getTaTravelById(record.getTaTravelId()));
        }

            return taTravelTargeList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public TaTravelTarge getTaTravelTargeById(Long id) {
        if (Objects.isNull(id)) {
            return new TaTravelTarge();
        }
        try {
            TaTravelTarge taTravelTarge = taTravelTargeMapper.selectById(id);
            taTravelTarge.setTaTravel(taTravelService.getTaTravelById(taTravelTarge.getTaTravelId()));
            if (Objects.isNull(taTravelTarge)){
                taTravelTarge = new TaTravelTarge();
            }
            return taTravelTarge;

        } catch (Exception e) {
            e.printStackTrace();
            return new TaTravelTarge();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param taTravelTarge 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByTaTravelTarge(TaTravelTarge taTravelTarge) {
        if (Objects.isNull(taTravelTarge)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<TaTravelTarge> queryWrapper = new LambdaQueryWrapper<>(new TaTravelTarge());
            

            if (!Objects.isNull(taTravelTarge.getId())) {
                queryWrapper.eq(TaTravelTarge::getId, taTravelTarge.getId());
            }

            if (!Objects.isNull(taTravelTarge.getTaTravelId())) {
                queryWrapper.eq(TaTravelTarge::getTaTravelId, taTravelTarge.getTaTravelId());
            }

            if (!Objects.isNull(taTravelTarge.getTarget())) {
                queryWrapper.like(TaTravelTarge::getTarget, taTravelTarge.getTarget());
            }

            if (!Objects.isNull(taTravelTarge.getStatus())) {
                queryWrapper.like(TaTravelTarge::getStatus, taTravelTarge.getStatus());
            }

            if (!Objects.isNull(taTravelTarge.getDelFlag())) {
                queryWrapper.like(TaTravelTarge::getDelFlag, taTravelTarge.getDelFlag());
            }

            if (!Objects.isNull(taTravelTarge.getCreateBy())) {
                queryWrapper.like(TaTravelTarge::getCreateBy, taTravelTarge.getCreateBy());
            }

            if (!Objects.isNull(taTravelTarge.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravelTarge.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravelTarge.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelTarge::getCreateTime, taTravelTarge.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravelTarge.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravelTarge::getCreateTime, taTravelTarge.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelTarge.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravelTarge.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravelTarge::getCreateTime, taTravelTarge.getParams().get("CreateTimeBeginTime"), taTravelTarge.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelTarge.getUpdateBy())) {
                queryWrapper.like(TaTravelTarge::getUpdateBy, taTravelTarge.getUpdateBy());
            }

            if (!Objects.isNull(taTravelTarge.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravelTarge.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravelTarge.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelTarge::getUpdateTime, taTravelTarge.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravelTarge.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravelTarge::getUpdateTime, taTravelTarge.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelTarge.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravelTarge.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravelTarge::getUpdateTime, taTravelTarge.getParams().get("UpdateTimeBeginTime"), taTravelTarge.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelTarge.getRemark())) {
                queryWrapper.like(TaTravelTarge::getRemark, taTravelTarge.getRemark());
            }

            List<TaTravelTarge> taTravelTarges = taTravelTargeMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravelTarge taTravelTargeNo:taTravelTarges){
                idList.add(taTravelTargeNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过taTravel外键idList查询idList
     *
     * @param taTravelIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByTaTravelIdList(List<Long> taTravelIdList) {
        if (Objects.isNull(taTravelIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<TaTravelTarge> queryWrapper = new LambdaQueryWrapper<>(new TaTravelTarge());
            queryWrapper.in(TaTravelTarge::getTaTravelId, taTravelIdList);
            List<TaTravelTarge> taTravelTarges = taTravelTargeMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravelTarge taTravelTarge:taTravelTarges){
                idList.add(taTravelTarge.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param taTravelTarge 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertTaTravelTarge(TaTravelTarge taTravelTarge) {
        if (Objects.isNull(taTravelTarge)) {
            return -1;
        }
        try {
        
            //taTravelTarge.setSpell(PinYin.getPinYin(taTravelTarge.getName()));
            return taTravelTargeMapper.insert(taTravelTarge);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param taTravelTarge 实体类
     * @return 结果
     */
    @Override
    public int updateTaTravelTargeById(TaTravelTarge taTravelTarge) {
        if (Objects.isNull(taTravelTarge)) {
            return -1;
        }
        try {
            //taTravelTarge.setSpell(PinYin.getPinYin(taTravelTarge.getName()));
            return taTravelTargeMapper.updateById(taTravelTarge);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteTaTravelTargeByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return taTravelTargeMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<TaTravelTarge> getTaTravelTargeList() {
        try {
            LambdaQueryWrapper<TaTravelTarge> queryWrapper = new LambdaQueryWrapper<>(new TaTravelTarge());
            queryWrapper.isNotNull(TaTravelTarge::getId);
            return taTravelTargeMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
